﻿using Framework.DI;
using Microsoft.AspNetCore.Http;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Admin.Application.DynamicApi.System
{
	public class UserManager : ISelfSingletonAutoInject
	{
		private readonly IHttpContextAccessor _httpContextAccessor;

		public UserManager(IHttpContextAccessor httpContextAccessor)
		{
			_httpContextAccessor = httpContextAccessor;
		}

		public long UserId
		{
			get => long.Parse(_httpContextAccessor.HttpContext?.User.FindFirst(ClaimConst.UserId)?.Value);
		}

		public string Account
		{
			get => _httpContextAccessor.HttpContext?.User.FindFirst(ClaimConst.Account)?.Value;
		}

		public string Name
		{
			get => _httpContextAccessor.HttpContext?.User.FindFirst(ClaimConst.Name)?.Value;
		}

		public bool SuperAdmin
		{
			get => _httpContextAccessor.HttpContext?.User.FindFirst(ClaimConst.Account)?.Value == "superadmin";
		}

		public long OrgId
		{
			get
			{
				var orgId = _httpContextAccessor.HttpContext?.User.FindFirst(ClaimConst.OrgId)?.Value;
				return string.IsNullOrWhiteSpace(orgId) ? 0 : long.Parse(orgId);
			}
		}

		public string OrgName
		{
			get => _httpContextAccessor.HttpContext?.User.FindFirst(ClaimConst.OrgName)?.Value;
		}
	}
}
